package com.beone.admin.controller.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.base.common.beans.Result;
import com.beone.admin.controller.app.common.BaseAppController;
import com.beone.admin.entity.Task;
import com.beone.admin.form.TaskForm;
import com.beone.admin.query.TaskQuery;
import com.beone.admin.service.TaskService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 任务控制器
 * </p>
 *
 * @author Pagenny
 * @date 2019-05-19 10:55
 **/
@RestController("appTaskController")
@RequestMapping("/api/task")
@Api(value = "APP任务", tags = {"APP端"})
public class TaskController extends BaseAppController {

    @Autowired
    private TaskService taskService;

    @ApiOperation("任务列表检索")
    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    public Object showList(TaskQuery query) {
        query.setUserId(getUserId());
        return taskService.getTaskPagination(query);
    }

    @ApiOperation(value = "保存任务反馈", notes = "保存任务反馈 主键属性为空新增，否则修改")
    @PostMapping(value = "/save")
    public Result save(@RequestBody TaskForm form) {
        form.setUserId(super.getUserId());
        if(taskService.insertOrUpdate(form)){
            return Result.success();
        }
        return Result.failure("保存失败");
    }

    @ApiOperation(value = "删除任务", notes = "根据id删除任务")
    @PostMapping(value = "/delete")
    @ApiImplicitParam(name = "taskId", value = "任务id", required = true, paramType = "param", dataType = "string")
    public Result delete(Integer taskId) {
        Task task = taskService.selectById(taskId);
        if (task == null) {
            return Result.failure("没有找到指定任务");
        }
        if (!getUserId().equals(task.getCreateUserId())) {
            return Result.failure("没有权限操作");
        }
        return Result.success(taskService.deleteById(taskId));
    }

    @ApiOperation(value = "任务统计概览", notes = "任务统计概览（当天，本周，本月）")
    @GetMapping(value = "/statistic")
    public Result statistic() {
        Integer userId = super.getUserId();
        return Result.success(taskService.getStatistics(userId));
    }
}
